package com.qs.education.controllers;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.qs.education.bean.api.PageBean;
import com.qs.education.bean.api.ResponseBean;
import com.qs.education.bean.api.ResponseListBean;
import com.qs.education.bean.beans.EducationTeacherResponse;
import com.qs.education.bean.entity.EducationTeacher;
import com.qs.education.bean.enums.ResponseStatus;
import com.qs.education.bean.utils.DateUtil;
import com.qs.education.bean.utils.JsonUtil;
import com.qs.education.service.ITeacherAPIService;

/**
 * @class TeacherController
 * @author Leon
 * @description
 * @date Nov 1, 2018
 */
@RestController
@RequestMapping("/teacher")
public class TeacherController {
	@Autowired
	private ITeacherAPIService teacherAPIServiceImpl;

	private final Logger log = Logger.getLogger(this.getClass());

	/**
	 * @Description 根据手机号 查询用户是否存在
	 * @author Leon
	 * @date Nov 5, 2018
	 * @param
	 * @return EducationTeacherResponse
	 */
	@RequestMapping("/findTeacherByMobile")
	public EducationTeacherResponse findTeacherByMobile(HttpServletRequest request, String mobile) {
		log.info("api Teacher 端 Controller  findTeacherByMobile");
		EducationTeacherResponse response = new EducationTeacherResponse();
		response = teacherAPIServiceImpl.findTeacherByMobile(mobile);
		response.setToken(response.getToken(request));
		return response;
	}

	/**
	 * @Description 根据手机号或者姓名进行查询
	 * @author Leon
	 * @date Nov 12, 2018
	 * @param
	 * @return ResponseListBean<EducationTeacher>
	 */
	@RequestMapping("/findTeacherByMobileOrName")
	public ResponseListBean<EducationTeacher> findTeacherByMobileOrName(HttpServletRequest request, String content) {
		System.out.println("api controller ========================");
		System.out.println("content: " + content);
		log.info("api Teacher controller findTeacherByMobileOrName");
		ResponseListBean<EducationTeacher> list = new ResponseListBean<EducationTeacher>();
		list = teacherAPIServiceImpl.findTeacherByMobileOrName(content);
		list.setToken(list.getToken(request));
		return list;
	}

	/**
	 * @Description 根据手机号和密码进行登录
	 * @author Leon
	 * @date Nov 6, 2018
	 * @param
	 * @return EducationTeacherResponse
	 */
	@RequestMapping("/teacherLogin")
	public EducationTeacherResponse login(HttpServletRequest request, String mobile, String password) {
		log.info("api Teacher 端 Controller  login");
		EducationTeacherResponse response = new EducationTeacherResponse();
		response = teacherAPIServiceImpl.login(mobile, password);
		response.setToken(response.getToken(request));
		return response;
	}

	/**
	 * @Description 退出登录
	 * @author Leon
	 * @date Nov 7, 2018
	 * @param
	 * @return EducationTeacherResponse
	 */
	@RequestMapping("/teacherQuit")
	public EducationTeacherResponse teacherQuit(HttpServletRequest request, String mobile) {
		log.info("api Teacher端 Controller teacherQuit");
		EducationTeacherResponse response = new EducationTeacherResponse();
		response = teacherAPIServiceImpl.quit(mobile);
		response.setToken(response.getToken(request));
		return response;
	}

	/**
	 * @Description 教师端 忘记密码
	 * @author Leon
	 * @date Nov 6, 2018
	 * @param
	 * @return EducationTeacherResponse
	 */
	@RequestMapping("/forgetPassword")
	public EducationTeacherResponse forgetPassword(HttpServletRequest request, String mobile, String password) {
		log.info("api Teacher 端 Controller forgetPassword");
		EducationTeacherResponse response = new EducationTeacherResponse();
		response = teacherAPIServiceImpl.forgetPassword(mobile, password);
		response.setToken(response.getToken(request));
		return response;
	}

	/**
	 * @Description 注册成功保存 用户手机号和密码
	 * @author Leon
	 * @date Nov 7, 2018
	 * @param
	 * @return EducationTeacherResponse
	 */
	@RequestMapping("/saveTeacherMobileAndPassword")
	public EducationTeacherResponse saveTeacherMobileAndPassword(HttpServletRequest request, String mobile,
			String password) {
		log.info("api Teacher 端 controller saveTeacherMobileAndPassword");
		System.out.println("==========================mobile,password" + mobile + " " + password);
		EducationTeacherResponse response = new EducationTeacherResponse();
		response = teacherAPIServiceImpl.saveTeacherMobileAndPassword(mobile, password);
		response.setToken(response.getToken(request));
		return response;
	}

	/**
	 * @Description 完善教师信息
	 * @author Leon
	 * @date Nov 8, 2018
	 * @param
	 * @return EducationTeacherResponse
	 */
	@RequestMapping("/perfectTeacherInfo")
	public EducationTeacherResponse perfectTeacherInfo(HttpServletRequest request, EducationTeacher teacher,
			Integer schoolId, Integer subjectId) {
		EducationTeacherResponse response = new EducationTeacherResponse();
		response = teacherAPIServiceImpl.perfectTeacherInfo(teacher, schoolId, subjectId);
		response.setToken(response.getToken(request));
		return response;
	}
	
	/**
	 * @Description 退出群组
	 * @author Leon
	 * @date   Nov 19, 2018
	 * @param  
	 * @return ResponseBean
	 */
	@RequestMapping("/quitGroup")
	public ResponseBean quitGroup(HttpServletRequest request,String groupId,String teacherId) {
		ResponseBean response = new ResponseBean();
		response = teacherAPIServiceImpl.quitGroup(groupId, teacherId);
		response.setToken(response.getToken(request));
		return response;
	}
	// ==================================================================

	/**
	 * @Description 查询所有符合条件的老师
	 * @author Jason
	 * @date Nov 20, 2018
	 * @param request
	 * @param teacher
	 * @param currentPage
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("/getTeacherAPI")
	public PageBean getTeacher(HttpServletRequest request,
			String teacher,Integer currentPage ,Integer pageSize) {

		log.info("api Teacher 端 Controller  getTeacherAPI");
		EducationTeacher educationTeacher = (EducationTeacher) JsonUtil.jsonToBean(teacher, EducationTeacher.class);
		PageBean response= new PageBean();
		response.setToken(response.getToken(request));
		response = teacherAPIServiceImpl.getEducationTeacher(educationTeacher,currentPage,pageSize);
		response.setStatus(ResponseStatus.SUCCESS.toString());

		return response;
	}

	/**
	 * @Description 根据ID删除老师
	 * @author Jason
	 * @date Nov 6, 2018
	 * @param request
	 * @return
	 */
	@RequestMapping("/deleteTeacherByIdAPI")
	public EducationTeacherResponse deleteTeacherById(HttpServletRequest request, Integer teacherId) {
		log.info("api Teacher 端 Controller  deleteTeacherById");
		EducationTeacherResponse response = new EducationTeacherResponse();
		response.setToken(response.getToken(request));
		Boolean result = teacherAPIServiceImpl.deleteTeacherById(teacherId);
		if (result) {
			response.setStatus(ResponseStatus.SUCCESS.toString());
		} else {
			response.setStatus(ResponseStatus.FAILED.toString());
		}
		return response;
	}

	/**
	 * @Description 根据ID查询老师
	 * @author Jason
	 * @date Nov 7, 2018
	 * @param request
	 * @param teacherId
	 * @return
	 */
	@RequestMapping("/findTeacherByIdAPI")
	public EducationTeacherResponse findTeacherById(HttpServletRequest request, Integer teacherId) {

		log.info("api Teacher 端 Controller  findTeacherById");
		EducationTeacherResponse response = new EducationTeacherResponse();
		response.setToken(response.getToken(request));
		response = teacherAPIServiceImpl.findTeacherById(teacherId);
		response.setStatus(ResponseStatus.SUCCESS.toString());
		return response;
	}

	/**
	 * 
	 * @Description 修改老师信息
	 * @author Jason
	 * @date Nov 7, 2018
	 * @param request
	 * @param teacherId
	 * @return
	 */
	@RequestMapping("/updateTeacherAPI")
	public EducationTeacherResponse updateTeacher(HttpServletRequest request, String educationTeacher) {

		log.info("api Teacher 端 Controller  updateTeacher");
		EducationTeacher teacher = (EducationTeacher) JsonUtil.jsonToBean(educationTeacher, EducationTeacher.class);
		EducationTeacherResponse response = new EducationTeacherResponse();
		response.setToken(response.getToken(request));
		Boolean result = teacherAPIServiceImpl.updateTeacher(teacher);
		if (result) {
			response.setStatus(ResponseStatus.SUCCESS.toString());
		} else {
			response.setStatus(ResponseStatus.FAILED.toString());
		}

		return response;
	}

	/**
	 * @Description 添加老师信息
	 * @author Jason
	 * @date Nov 9, 2018
	 * @param request
	 * @param teacherId
	 * @return
	 */
	@RequestMapping("/addTeacherAPI")
	public EducationTeacherResponse addTeacher(HttpServletRequest request, String educationTeacher) {

		log.info("api Teacher 端 Controller  updateTeacher");
		EducationTeacher teacher = (EducationTeacher) JsonUtil.jsonToBean(educationTeacher, EducationTeacher.class);
		EducationTeacherResponse response = new EducationTeacherResponse();
		response.setToken(response.getToken(request));
		teacher.setCreateTime(DateUtil.dateToString(new Date(), "yyyy-MM-dd HH:mm:ss"));
		Boolean result = teacherAPIServiceImpl.addTeacher(teacher);
		if (result) {
			response.setStatus(ResponseStatus.SUCCESS.toString());
		} else {
			response.setStatus(ResponseStatus.FAILED.toString());
		}

		return response;
	}

	/**
	 * @Description 根据条件搜索老师
	 * @author Jason
	 * @date Nov 12, 2018
	 * @param request
	 * @param educationTeacher
	 * @return
	 */
	@RequestMapping("/findTeacherByConditionAPI")
	public ResponseListBean<EducationTeacher> findTeacherByCondition(HttpServletRequest request,
			String educationTeacher) {

		log.info("api Student 端 Controller  findTeacherByCondition");
		EducationTeacher teacher = (EducationTeacher) JsonUtil.jsonToBean(educationTeacher, EducationTeacher.class);
		ResponseListBean<EducationTeacher> responseList = new ResponseListBean<EducationTeacher>();
		responseList.setToken(responseList.getToken(request));
		responseList = teacherAPIServiceImpl.findTeacherByCondition(teacher);
		responseList.setStatus(ResponseStatus.SUCCESS.toString());

		return responseList;
	}

	/**
	 * @Description 修改教师信息
	 * @author Leon
	 * @date Nov 15, 2018
	 * @param
	 * @return EducationTeacherResponse
	 */
	@RequestMapping("/modifyTeacherInfo")
	public EducationTeacherResponse modifyTeacherInfo(HttpServletRequest request, String bean,
			@RequestParam(value = "file", required = false) MultipartFile file,
			@RequestParam(value = "url", required = false) String url) {
		log.info("api controller controller");
		EducationTeacherResponse response = new EducationTeacherResponse();
		EducationTeacher teacherBean = (EducationTeacher) JsonUtil.jsonToBean(bean, EducationTeacher.class);
		try {
			response = teacherAPIServiceImpl.modifyTeacherInfo(teacherBean, file, url);
		} catch (Exception e) {
			response.setStatus(ResponseStatus.FAILED.toString());
			e.printStackTrace();
		}
		response.setToken(response.getToken(request));
		return response;
	}
	
	/**
	 * @Description 修改密码
	 * @author Leon
	 * @date   Nov 27, 2018
	 * @param  
	 * @return ResponseBean
	 */
	@RequestMapping("/changeTeacherPassword")
	public ResponseBean changePassword(HttpServletRequest request,String id,String oldPassword,String newPassword) {
		ResponseBean response = new ResponseBean();
		try {
			response = teacherAPIServiceImpl.changePassword(id, oldPassword, newPassword);
		} catch (Exception e) {
			response.setStatus(ResponseStatus.FAILED.toString());
			response.setErrorCode("2");
			response.setMessage("密码修改失败，请重新尝试");
			e.printStackTrace();
		}
		return response;
	}
}
